$(function() {
	function getFields() {
		$
				.ajax({
					url : BASE_URL_FIELD_SEARCH
							+ '/findAllOfCurrentUserCanManage' + '?v='
							+ Math.random(),
					type : "post",
					dataType : 'json',
					success : function(data) {
						var html = '';
						if (data && data.content) {
							for (var i = 0; i < data.content.length; i++) {
								html += '<input class="chboxField" type="checkbox" data-fieldid="'
										+ data.content[i].id
										+ '" data-field="'
										+ data.content[i].name
										+ '" checked="checked"/><span>'
										+ data.content[i].name + '</span>';
							}
							$("#fields-con").append(html);
							refreshCharts();
						} else {
							refreshCharts();
						}
					},
					error : function() {
						$.messager.alert('错误', '<p>服务异常，请稍后重试或联系管理员!</p>',
								'error');
					}
				});
	}
	function refreshCharts() {
		// 是否选择全部
		var selectAll = $(".all_join").is(":checked");
		var dateType = $("input[name=date_input]:checked").val();
		var dataArr = {};
		var categories;
		var period = dateType;
		var fields = [];
		$(".chboxField").each(function() {
			if ($(this).is(":checked")) {
				fields.push({
					id : $(this).attr("data-fieldid"),
					name : $(this).attr("data-field")
				});
			}
		});

		// 周期
		var period = dateType;
		var postData = {
			"selectAll" : selectAll,
			"fields" : fields,
			"period" : period
		};
		$
				.ajax({
					url : BASE_URL_API
							+ '/pfThroughput/findPfThroughputInfo?v='
							+ Math.random(),
					type : "post",
					dataType : 'json',
					contentType : "application/json",
					data : JSON.stringify(postData),
					success : function(data) {
						var options = $('#container').highcharts().options;
						var series = [];
						var all_checked = $('.chboxField').siblings(
								'input[type="checkbox"]').is(":checked");
						if (!data.value) {
							for (var i = 0; i < postData.fields.length; i++) {
								series.push({
									name : postData.fields[i].name,
									data : []
								});
								options.xAxis[0].categories = [];
							}
						} else {
							for (var i = 0; i < data.value.series.length; i++) {
								var dataArr = [];
								for (var j = 0; j < data.value.series[i].values.length; j++) {
									var value = +data.value.series[i].values[j];
									dataArr.push(value);
								}
								series.push({
									name : data.value.series[i].name,
									data : dataArr
								});
								options.xAxis[0].categories = data.value.categories.values;
								// alert(data.value.series[i].name)
								// alert(data.value.categories.values)
							}
						}
						if (!all_checked) {
							var nulls = " ";
							series.push({
								name : nulls,
								data : nulls
							});
							options.xAxis[0].categories
							var reaArr = [];
							dateType == "0"
						}
						if (dateType == "3") {
							options.xAxis[0].labels.formatter = function() {
								return this.value.split(" ")[0];
							}
						} else if (dateType == "4") {
							options.xAxis[0].labels.formatter = function() {
								var dateA = this.value.split(" ")[0];
								return dateA.split("-")[1] + "-"
										+ dateA.split("-")[2]
							}
						} else {
							options.xAxis[0].labels.formatter = function() {
								var dateArr = this.value.split("-");
								return dateArr[0]
										+ (dateArr[1] ? "-"
												+ this.value.split("-")[1] : "");
							}
						}

						options.series = series;
						var str = options.xAxis[0].categories;
						$('#container').highcharts().destroy();
						// console.log(options);
						$('#container').highcharts(options);
						// 成功
						/*
						 * var json=JSON.parse(data);
						 * categories=json.categories.values;
						 * $.each(json.serie,function(i,item){
						 * dataArr[item.name]=item.values;
						 * 
						 * });
						 */
						// 失败
					},
					error : function() {
						$.messager.alert('错误', '<p>服务异常，XXXX，请稍后重试或联系管理员!</p>',
								'error');
					}
				});
	}

	$("#fields-con").delegate(".chboxField", "click", function() {
		refreshCharts();
	});
	$("input[name=date_input]").click(function() {
		refreshCharts();
	});

	function gets() {
		$
				.ajax({
					url : BASE_URL_FIELD_SEARCH
							+ '/findAllOfCurrentUserCanManage' + '?v='
							+ Math.random(),
					type : "post",
					dataType : 'json',
					success : function(data) {
						var html = '';
						if (data && data.content) {
							for (var i = 0; i < data.content.length; i++) {
								html += '<input class="chboxField_one" type="checkbox" data-fieldid="'
										+ data.content[i].id
										+ '" data-field="'
										+ data.content[i].name
										+ '" checked="checked"/><span>'
										+ data.content[i].name + '</span>';
							}
							$("#fields-con_one").append(html);
							refreshChart();
						}
					},
					error : function() {
						$.messager.alert('错误', '<p>服务异常，XXXX，请稍后重试或联系管理员!</p>',
								'error');
					}
				});
	}

	function refreshChart() {
		var dateType = $("input[name=date_input_one]:checked").val();
		var dataArr = {};
		var categories;
		var period = dateType;
		var fields = [];
		/*
		 * if(dateType == "0") { var fields =
		 * '{"id":"40287d815180f8bf015180faa0360002","name":"series1"}, ' +
		 * '{"id":"40287d815180f8bf015180faa2e80004","name":"series2"}'; }
		 */

		$(".chboxField_one").each(function() {
			if ($(this).is(":checked")) {
				fields.push({
					id : $(this).attr("data-fieldid"),
					name : $(this).attr("data-field")
				});
			}
		});
		// 是否选择全部
		var selectAll = $(".all_join_one").is(":checked");

		// 周期
		var period = dateType;
		var postData = {
			"selectAll" : selectAll,
			"fields" : fields,
			"period" : period
		};
		$
				.ajax({
					url : BASE_URL_API
							+ '/pfThroughput/findPfThroughputRateInfo?v='
							+ Math.random(),
					type : "post",
					dataType : 'json',
					contentType : "application/json",
					data : JSON.stringify(postData),
					success : function(data) {
						var options = $('#container_one').highcharts().options;
						var series = [];
						var all_checked = $('.chboxField_one').siblings(
								'input[type="checkbox"]').is(":checked");

						if (!data.value) {
							for (var i = 0; i < postData.fields.length; i++) {
								series.push({
									name : postData.fields[i].name,
									data : []
								});
								options.xAxis[0].categories = [];
							}
						} else {
							for (var i = 0; i < data.value.series.length; i++) {
								series.push({
									name : data.value.series[i].name,
									data : data.value.series[i].values
								});
								options.xAxis[0].categories = data.value.categories.values;
							}
						}

						if (!all_checked) {
							var nulls = " ";
							series.push({
								name : nulls,
								data : nulls
							});
							options.xAxis[0].categories
						}
						var reaArr = [];
						if (dateType == "3") {
							options.xAxis[0].labels.formatter = function() {
								return this.value.split(" ")[0];
							}
						} else if (dateType == "4") {
							options.xAxis[0].labels.formatter = function() {
								var dateA = this.value.split(" ")[0];
								return dateA.split("-")[1] + "-"
										+ dateA.split("-")[2]
							}
						} else {
							options.xAxis[0].labels.formatter = function() {
								var dateArr = this.value.split("-");
								return dateArr[0]
										+ (dateArr[1] ? "-"
												+ this.value.split("-")[1] : "");
							}
						}

						options.series = series;
						var str = options.xAxis[0].categories;
						$('#container_one').highcharts().destroy();
						// console.log(options);
						$('#container_one').highcharts(options);
						// 成功
						/*
						 * var json=JSON.parse(data);
						 * categories=json.categories.values;
						 * $.each(json.serie,function(i,item){
						 * dataArr[item.name]=item.values;
						 * 
						 * });
						 */
						// 失败
					},
					error : function() {
						$.messager.alert('错误', '<p>服务异常，XXXX，请稍后重试或联系管理员!</p>',
								'error');
					}
				});
	}
	;

	$("#fields-con_one").delegate(".chboxField_one", "click", function() {
		refreshChart();
	});
	$("input[name=date_input_one]").click(function() {
		refreshChart();
	});

	$('#container').highcharts(
			{

				credits : {
					enabled : false
				},
				title : {
					text : '平台吞吐量',
					x : -20
				// center
				},
				subtitle : {
					text : '',
					x : -20
				},
				xAxis : {
					// categories:['1月', '2月', '3月', '4月', '5月', '6月','7月',
					// '8月', '9月', '10月', '11月', '12月'],
					labels : {
						formatter : function() {
							var dateArr = this.value.split("-");
							return dateArr[0]
									+ (dateArr[1] ? "-"
											+ this.value.split("-")[1] : "");
						}
					}
				/*
				 * type: 'datetime', dateTimeLabelFormats: { month: '%Y-%m' }
				 */
				},
				yAxis : {
					min : 0,
					labels : {
						formatter : function() {
							return this.value + 'MB';
						}
					},
					title : {
						text : '单位(MB)'
					},
					plotLines : [ {
						value : 0,
						width : 1,
						color : '#808080'
					} ]
				},
				tooltip : {
					valueSuffix : 'MB'
				},
				legend : {
					layout : 'vertical',
					align : 'right',
					verticalAlign : 'middle',
					borderWidth : 0
				},
				series : []
			});
	getFields();

	$('#container_one').highcharts(
			{
				chart : {
					width : document.body.offsetWidth - 100

				},
				credits : {
					enabled : false
				},
				title : {
					text : '平台吞吐率',
					x : -20
				// center
				},
				subtitle : {
					text : '',
					x : -20
				},
				xAxis : {
					// categories: ['1月', '2月', '3月', '4月', '5月', '6月','7月',
					// '8月', '9月', '10月', '11月', '12月']
					labels : {
						formatter : function() {
							var dateArr = this.value.split("-");
							return dateArr[0]
									+ (dateArr[1] ? "-"
											+ this.value.split("-")[1] : "");
						}
					}
				},
				yAxis : {
					min : 0,
					labels : {
						formatter : function() {
							return this.value + "MB";
						}
					},
					title : {
						text : '单位(MB)'
					},
					plotLines : [ {
						value : 0,
						width : 1,
						color : '#808080'
					} ]
				},
				tooltip : {
					valueSuffix : 'MB'
				},
				legend : {
					layout : 'vertical',
					align : 'right',
					verticalAlign : 'middle',
					borderWidth : 0
				},
				series : []
			});
	gets()
});
